package com.zeng.sim.util;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zeng.sim.mapper.model.SysConfigEntity;
import com.zeng.sim.service.SysConfigService;
import org.springframework.context.ApplicationContext;

/**
 * 描述信息
 *
 * @author zengwanlin
 * @date 2023-12-22T17:21
 */
public class SysConfigUtil {

    public static final String CONFIG_TYPE_SYSTEM = "system";

    public static final String CONFIG_TYPE_CUSTOM = "custom";

    public static final String CONFIG_CODE_CLIENT_SIM_SERVER_HOSTNAME = "client_sim_server_hostname";

    public static final String CONFIG_CODE_CLIENT_SIM_SERVER_PORT = "client_sim_server_port";

    public static final String CONFIG_CODE_CLIENT_SIM_SERVER_URL = "client_sim_server_url";

    public static final String CONFIG_CODE_SERVER_SIM_SOCKET_ENABLE = "server_sim_socket_enable";

    public static final String CONFIG_CODE_SERVER_SIM_SOCKET_LISTEN_PORT = "server_sim_socket_listen_port";

    public static final String CONFIG_CODE_SERVER_SIM_DEFAULT_CHARSET = "server_sim_default_charset";

    public static final String CONFIG_CODE_SERVER_SIM_CHANNEL_NO_XPATH = "server_sim_channel_no_xpath";

    public static final String CONFIG_CODE_SERVER_SIM_CHANNEL_NO_JSONPATH = "server_sim_channel_no_jsonPath";

    public static final String CONFIG_CODE_SERVER_SIM_MSG_CODE_XPATH = "server_sim_msg_code_xpath";

    public static final String CONFIG_CODE_SERVER_SIM_MSG_CODE_JSONPATH = "server_sim_msg_code_jsonPath";

    public static final String CONFIG_CODE_SIM_BUSI_POOL_CORE_SIZE = "sim_busi_pool_core_size";

    /**
     * 获取系统配置
     * @param configCode
     * @return
     */
    public static String getSystemConfigValue(String configCode) {
        return getConfigValue(CONFIG_TYPE_SYSTEM, configCode);
    }

    /**
     * 获取用户配置
     * @param configCode
     * @return
     */
    public static String getCustomConfigValue(String configCode) {
        return getConfigValue(CONFIG_TYPE_CUSTOM, configCode);
    }

    /**
     * 获取配置
     * @param configType
     * @param configCode
     * @return
     */
    public static String getConfigValue(String configType, String configCode) {
        SysConfigService sysConfigService = getSysConfigService();
        LambdaQueryWrapper<SysConfigEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysConfigEntity::getConfigType, configType);
        wrapper.eq(SysConfigEntity::getConfigCode, configCode);
        SysConfigEntity sysConfigEntity = sysConfigService.getOne(wrapper);
        Assert.notNull(sysConfigEntity, "缺失系统配置，标识[{}]，类型[{}]", configCode, configType);
        return sysConfigEntity.getConfigValue();
    }

    /**
     * 获取配置
     * @param configType
     * @param configCode
     * @param context
     * @return
     */
    public static String getConfigValue(String configType, String configCode, ApplicationContext context) {
        SysConfigService sysConfigService = context.getBean(SysConfigService.class);
        LambdaQueryWrapper<SysConfigEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysConfigEntity::getConfigType, configType);
        wrapper.eq(SysConfigEntity::getConfigCode, configCode);
        SysConfigEntity sysConfigEntity = sysConfigService.getOne(wrapper);
        Assert.notNull(sysConfigEntity, "缺失系统配置，标识[{}]，类型[{}]", configCode, configType);
        return sysConfigEntity.getConfigValue();
    }

    private static SysConfigService getSysConfigService() {
        return ApplicationContextUtil.getBean(SysConfigService.class);
    }

}
